<!--
  * 数据 字典
  *
  * @Author:    1024创新实验室-主任：卓大
  * @Date:      2022-06-08 21:50:41
  * @Wechat:    zhuda1024
  * @Email:     lab1024@163.com
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012
-->
<template>
    <a-form class="smart-query-form">
        <a-row class="smart-query-form-row">

            <a-form-item label="所属医馆" class="smart-query-form-item">
                <DoctorSelect v-model:value="queryForm.appid"/>
            </a-form-item>
            <a-form-item label="医生名称" class="smart-query-form-item">
                <a-input style="width: 200px" v-model:value="queryForm.name" placeholder="医生名称"/>
            </a-form-item>
            <a-form-item label="医生电话" class="smart-query-form-item">
                <a-input style="width: 200px" v-model:value="queryForm.tel" placeholder="医生电话"/>
            </a-form-item>

            <a-form-item class="smart-query-form-item smart-margin-left10">
                <a-button type="primary" @click="ajaxQuery">
                    <template #icon>
                        <ReloadOutlined/>
                    </template>
                    查询
                </a-button>
                <a-button @click="resetQuery">
                    <template #icon>
                        <SearchOutlined/>
                    </template>
                    重置
                </a-button>
            </a-form-item>
        </a-row>
    </a-form>

    <a-card size="small" :bordered="false" :hoverable="true">
        <a-row class="smart-table-btn-block">
            <div class="smart-table-operate-block">
                <a-button @click="showBusinessesDoctorDrawer" type="primary" size="small">
                    <template #icon>
                        <PlusOutlined/>
                    </template>
                    新增医生信息
                </a-button>
            </div>
            <div class="smart-table-setting-block">
                <TableOperator class="smart-margin-bottom5" v-model="columns" :tableId="TABLE_ID_CONST.SUPPORT.DICT"
                               :refresh="ajaxQuery"/>
            </div>
        </a-row>

        <a-table
                size="small"
                :dataSource="tableData"
                :columns="columns"
                :loading="tableLoading"
                rowKey="id"
                :pagination="false"
                bordered
        >
            <template #bodyCell="{ record, column }">
                <template v-if="column.dataIndex === 'name'">
                    <!--        <a @click="showInfirmaryStationModalList(record.appid)">{{ record.name }}</a>-->
                </template>
                <template v-else-if="column.dataIndex === 'icon'">
                    <!-- <div class="smart-table-operate"> -->
                    <!-- <a-image:width="200" :src={{record.pic}}/> -->
                    <!-- <a-empty v-if="record.icon !== ''"/> -->
                    <a-image :src="record.icon" style="max-width: 50px; max-height: 50px;"/>

                    <!-- </div> -->
                </template>

                <template v-if="column.dataIndex === 'status'">
                    <a-tag :color="record.status  ? 'error' : 'processing'">{{
                        record.status ? '禁用' : '正常'
                        }}
                    </a-tag>
                </template>

                <template v-if="column.dataIndex === 'is_admin'">
                    <a-tag :color="record.is_admin? 'error' : 'processing'">{{
                        record.is_admin ? '是管理员' : '非管理员'
                        }}
                    </a-tag>
                </template>

                <template v-if="column.dataIndex === 'is_rest'">
                    <a-tag :color="record.is_rest? 'purple' : 'green'">{{
                        record.is_rest ? '已休息' : '工作中'
                        }}
                    </a-tag>
                </template>

                <template v-else-if="column.dataIndex === 'action'">
                    <div class="smart-table-operate">
                        <a-button @click="showBusinessesDoctorDrawer(record)"
                                  type="link">编辑
                        </a-button>
                        <a-button @click="deleteBusinessesDoctor(record.id,record.name)"
                                   danger type="link">删除
                        </a-button>
                        <a-button @click="showInfirmaryForDoctorStationModalDrawer(record,record.appid)"
                                   type="link">排班配置
                        </a-button>
                        <a-button @click="showInfirmaryForDoctorSchedulingListModal(record,record.appid)" type="link">
                            排班管理
                        </a-button>
                        <a-button @click="showInfirmaryForDoctorSchedulingOrderListModal(record,record.appid)" type="link">
                            预约详情
                        </a-button>
                    </div>

                </template>
            </template>
        </a-table>

<!--        <div class="smart-query-table-page">-->
<!--            <a-pagination-->
<!--                    :pageSizeOptions="PAGE_SIZE_OPTIONS"-->
<!--                    :defaultPageSize="queryForm.pageSize"-->
<!--                    v-model:current="queryForm.pageNum"-->
<!--                    v-model:pageSize="queryForm.pageSize"-->
<!--                    :total="total"-->
<!--                    @change="ajaxQuery"-->
<!--                    @showSizeChange="ajaxQuery"-->
<!--                    :show-total="(total) => `共${total}条`"-->
<!--            />-->
<!--        </div>-->

        <div class="smart-query-table-page">
            <a-pagination
                showSizeChanger
                showQuickJumper
                show-less-items
                :pageSizeOptions="PAGE_SIZE_OPTIONS"
                :defaultPageSize="queryForm.pageSize"
                v-model:current="queryForm.pageNum"
                v-model:pageSize="queryForm.pageSize"
                :total="total"
                @change="ajaxQuery"
                @showSizeChange="ajaxQuery"
                :show-total="(total) => `共${total}条`"
            />
        </div>

        <InfirmaryStationListmodal ref="infirmaryStationListmodal"/>
        <!-- 医馆信息编辑或添加弹窗
        @refresh="ajaxQuery" 通知重父组件加载
        -->
        <InfirmaryForDoctorSchedulingListModal @refresh="ajaxQuery" ref="infirmaryForDoctorSchedulingListModal"/>
        <InfirmaryForDoctorSchedulingOrderListModal @refresh="ajaxQuery" ref="infirmaryForDoctorSchedulingOrderListModal"/>
        <DoctorFormModal ref="doctorFormModal" @refresh="ajaxQuery" />
        <DoctorSchedulingRuleFormModal @refresh="ajaxQuery" ref="doctorSchedulingRuleFormModal"/>
        <InfirmaryForDoctorStationModal @refresh="ajaxQuery" ref="infirmaryForDoctorStationModal"/>
    </a-card>
</template>
<script setup lang="ts">
import DictKeyOperateModal from './components/dict-key-operate-modal.vue';
import DictValueModal from './components/dict-value-modal.vue';
import InfirmaryForDoctorSchedulingListModal from './infirmary-for-doctor-scheduling-list-modal/index.vue';
import InfirmaryForDoctorSchedulingOrderListModal from './infirmary-for-doctor-scheduling-order-list-modal/index.vue';
import DoctorFormModal from './doctor-form-modal/index.vue';
import {reactive, ref, onMounted} from 'vue';
import {message, Modal} from 'ant-design-vue';
import {SmartLoading} from '/@/components/framework/smart-loading';
import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
import {businessesApi} from '/@/api/system/infirmary/businesses-api';
import {PAGE_SIZE_OPTIONS} from '/@/constants/common-const';
import {smartSentry} from '/@/lib/smart-sentry';
import TableOperator from '/@/components/support/table-operator/index.vue';
import {TABLE_ID_CONST} from '/@/constants/support/table-id-const';
import DoctorSelect from './doctor-select/index.vue';

import InfirmaryForDoctorStationModal from './infirmary-for-doctor-station-modal/index.vue';


const columns = ref([
    // {
    //   title: 'id',
    //   width: 90,
    //   dataIndex: 'id',
    // },
    {
        title: '医馆编号',
        dataIndex: 'appid',
    },
    {
        title: '医馆名称',
        dataIndex: 'businesses_name',
    },
    // {
    //     title: '医生编号',
    //     dataIndex: 'dc_number',
    // },
    {
        title: '医生名称',
        dataIndex: 'name',
    },
    {
        title: '医生图像',
        dataIndex: 'icon',
    },
    {
        title: '医生电话',
        dataIndex: 'tel',
    },
    {
        title: '是否管理员',
        dataIndex: 'is_admin',
    },
    {
        title: '是否休息',
        dataIndex: 'is_rest',
    },
    {
        title: '诊费(单位/元)',
        dataIndex: 'fee',
    },
    {
        title: '状态',
        dataIndex: 'status',
        width: 60,
    },
    {
        title: '操作',
        dataIndex: 'action',
        fixed: 'right',
        width: 300,
    },
]);

// ---------------- 查询数据 -----------------

const queryFormState = {
    appid: undefined,
    tel: undefined,
    name: undefined,
    pageNum: 1,
    pageSize: 10,
};
const queryForm = reactive({...queryFormState});
const tableLoading = ref(false);
const selectedRowKeyList = ref([]);
const tableData = ref([]);
const total = ref(0);
const operateModal = ref();
const dictValueModal = ref();
const infirmaryStationListmodal = ref();
const doctorFormModal = ref();
const doctorSchedulingRuleFormModal = ref();
const infirmaryForDoctorStationModal = ref();
const infirmaryForDoctorSchedulingListModal = ref();
const infirmaryForDoctorSchedulingOrderListModal = ref();

// 显示医生详情信息
function showInfirmaryBusinessesDoctorModalList(appid) {
    infirmaryStationListmodal.value.showModal(appid);
}

function showInfirmaryForDoctorStationModalDrawer(record, appid) {
    infirmaryForDoctorStationModal.value.showModal(record, appid);
}

function showInfirmaryForDoctorSchedulingListModal(record, appid) {
    infirmaryForDoctorSchedulingListModal.value.showModal(record, appid);
}


function showInfirmaryForDoctorSchedulingOrderListModal(record, appid) {
    infirmaryForDoctorSchedulingOrderListModal.value.showModal(record, appid);
}

// 展示医馆信息编辑弹窗---------------- 添加/更新 -----------------showInfirmaryBusinessesDoctorModalList
function showBusinessesDoctorDrawer(record) {
    console.log("你啥会啥啥啥")
    doctorFormModal.value.showDrawer(record);
}

function showBusinessesDoctorSchedulingRuleDrawer(record) {
    doctorSchedulingRuleFormModal.value.showDrawer(record);
}


// 删除医馆信息
function deleteBusinessesDoctor(id, name) {
    if (!id) {
        return;
    }
    Modal.confirm({
        title: '提示',
        content: '确定要删除该医生【' + name + '】信息么？',
        okText: '确定',
        okType: 'danger',
        async onOk() {
            SmartLoading.show();
            try {
                await businessesApi.deleteBusinessesDoctorUserByid(name, id);
                message.info('删除成功');
                // 重新刷新获取列表数据
                ajaxQuery();
            } catch (e) {
                smartSentry.captureError(e);
            } finally {
                SmartLoading.hide();
            }
        },
        cancelText: '取消',
        onCancel() {
        },
    });
}

function onSelectChange(selectedRowKeys) {
    selectedRowKeyList.value = selectedRowKeys;
}

function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
}

async function ajaxQuery() {
    try {
        tableLoading.value = true;
        let responseModel = await businessesApi.queryBusinessesDoctorList(queryForm);
        const list = responseModel.data.list;
        total.value = responseModel.data.total;
        tableData.value = list;
    } catch (e) {
        // smartSentry.captureError(e);
    } finally {
        tableLoading.value = false;
    }
}


// ---------------- 批量 删除 -----------------

function confirmBatchDelete() {
    Modal.confirm({
        title: '提示',
        content: '确定要删除选中Key吗?',
        okText: '删除',
        okType: 'danger',
        onOk() {
            // batchDelete();
        },
        cancelText: '取消',
        onCancel() {
        },
    });
}


onMounted(ajaxQuery);
</script>
